﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Triad.Controls;
using Triad.Model;
using Triad.BLL;

namespace Triad.Framework.Admin
{
    public partial class MenuMaintenance : Triad.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            tflv1.DataSource = new Triad.BLL.framework().getMenuItems("en-US", true);
            tflv1.DataBind();
        }

        protected void tflv1_OnUpdate(object sender, Triad.Controls.lvUpdateEventArgs e)
        {
            string[] EventTarget = e.EventTarget.Split('|');
            DataTable dtResults = e.tfDataset.Tables[0];
            e.RetVal = "false|";

            switch (EventTarget[0])
            {
                case "self":
                    e.RetVal = CommitMenuData(e.tfDataset.Tables[0]);
                    return;
                default:
                    break;
            }
        }

        /// <summary>
        /// Commits the menu data to the Database.
        /// </summary>
        /// <param name="dataTable">The data table holding information from the listview/.</param>
        /// <returns></returns>
        private string CommitMenuData(DataTable dataTable)
        {
            string strTemp = "";
            foreach (DataRow row in dataTable.Rows)
            {
                switch ((tfListView.RowType)row["_tfType"])
                {
                    case tfListView.RowType.ModifiedOriginalData: //Modified Original Data
                        strTemp = row.ItemArray.GetUpperBound(0).ToString();
                        break;
                    case tfListView.RowType.Selected: //Selected Row;
                        strTemp = row.ItemArray.GetUpperBound(0).ToString();
                        break;
                    case tfListView.RowType.Updated: //Modifed New Data;
                        new framework().UpdateMenuItem(Int32.Parse(row["MenuItemId"].ToString()),
                            row["MenuItemLabel"].ToString(),
                            Int32.Parse(row["ReferenceIdMenuItemLabel"].ToString()),
                            "en-US",
                            Convert.ToBoolean(Int32.Parse(row["Visible"].ToString())),
                            short.Parse(row["SortOrder"].ToString()),
                            row["TargetUrl"].ToString());
                        return "true|";
                    case tfListView.RowType.Deleted: //Deleted Record;
                        new framework().DeleteMenuItem(Int32.Parse(row["MenuItemId"].ToString()));
                        return "true|";
                    case tfListView.RowType.Added: //Added Row;
                        new framework().InsertMenuItem_USP(row["MenuItemLabel"].ToString(),
                            Int32.Parse(row["MenuItemIdParent"].ToString()),
                            Convert.ToBoolean(row["Visible"]),
                            short.Parse(row["SortOrder"].ToString()),
                            row["TargetUrl"].ToString());
                        return "true|";
                    default:
                        break;
                }
            }

            //// OR, you can do things this way...
            var newDT = new DataView(dataTable, "_edType=" + (int)tfListView.RowType.Selected, null, DataViewRowState.CurrentRows).ToTable();

            foreach (DataRow row in newDT.Rows)
            {
                // Do work on Selected Rows - Business Logic, etc...
            }

            
            return "false|";
        }
    }
}
